/**
 * tarside-ftp -- A simple FTP client, using the ANSI C and GTK+.
 * Copyright (C) 2011  degui.chen <degui.chen@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "retr.h"
#include "utility.h"
#include "info.h"
#include "local_dir.h"
#include "login.h"

static void retr_file_result(int df_connect, char* filename) {
	char* result = (char*) malloc(1024 * 10 * sizeof(char));
	int result_lenth = 0;
	char buffer[1024];
	int length;
	while (1) {
		length = recv(df_connect, buffer, 1024, 0);
		if (1 > length) {
			break;
		}
		buffer[length] = '\0';
		memmove(result + result_lenth, buffer, length);
		result_lenth += length;
	}
	result[result_lenth] = '\0';
	char path[256] = { '\0' };
	sprintf(path, "%s/%s", get_local_current_dir(), filename);
	file_put_contents(path, result);
	free(result);
	refresh_local_dir();
}

static void retr_file_info(int port, char* filename) {
	char message[256] = { '\0' };
	int bind_socket = open_bind(port);
	int address_length = sizeof(struct sockaddr_in);
	struct sockaddr_in address_client;
	int df_connect;
	sprintf(message, "accept socket %i port %i \n", bind_socket, port);
	set_commond_info(message);
	if ((df_connect = accept(bind_socket, (struct sockaddr *) &address_client, &address_length)) == -1) {
		perror("accept() error\n");
		exit(1);
	}
	sprintf(message, "You got a connection from %s\n", inet_ntoa(address_client.sin_addr));
	set_commond_info(message);
	retr_file_result(df_connect, filename);
	sprintf(message, "close connect %i \n", df_connect);
	set_commond_info(message);
	sprintf(message, "close connect %i \n", bind_socket);
	set_commond_info(message);
	close(df_connect);
	close(bind_socket);
}

void retr_comment(int socket_connect, char* filename) {
	char buffer[1024];
	char get[255] = { '\0' };
	sprintf(get, "RETR %s\r\n", filename);
	if (get_ftp_model()) {
		char ports[256] = { '\0' };
		create_port_comment(&ports);
		send_cmd(socket_connect, ports);
		recv_cmd(socket_connect, buffer, 1024);
		set_commond_info(buffer);

		send_cmd(socket_connect, get);
		recv_cmd(socket_connect, buffer, 1024);
		set_commond_info(buffer);

		retr_file_info(get_port(), filename);
	} else {
		char pasv[256] = "PASV \r\n";
		send_cmd(socket_connect, pasv);
		recv_cmd(socket_connect, buffer, 1024);
		set_commond_info(buffer);

		send_cmd(socket_connect, get);
		recv_cmd(socket_connect, buffer, 1024);
		set_commond_info(buffer);

		int port = get_pasv_port(buffer);
		retr_file_info(port, filename);
	}
}
